package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.camera.models.CameraBean;
import cn.innosmart.aq.util.DeviceConstant;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Created by David on 2015/7/30.
 */
public class DeviceListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;
    private ArrayList<String> addresses;
    private View.OnClickListener listener;
    private View.OnLongClickListener longClickListener;
    private AquariumBean aquariumBean;
    private boolean isModify;


    /**
     * @param context
     */
    public DeviceListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
        addresses = new ArrayList<String>();
    }

    @Override
    public int getCount() {
        return addresses.size();
    }

    @Override
    public Object getItem(int position) {
        return addresses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_device_list, null);
            holder.ivDeviceIcon = (ImageView) convertView.findViewById(R.id.iv_device_icon);
            holder.tvDeviceName = (TextView) convertView.findViewById(R.id.tv_device_name);
            holder.rl_modify = (RelativeLayout) convertView.findViewById(R.id.rl_modify);
            holder.iv_delete_device = (ImageView) convertView.findViewById(R.id.iv_delete_device);
            holder.iv_modify_device = (ImageView) convertView.findViewById(R.id.iv_modify_device);
            holder.rlBg = (RelativeLayout) convertView.findViewById(R.id.rl_bg);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DeviceBean deviceBean = null;
        String address = addresses.get(position);
        //兼容虚拟Web设备
        if (address.contains("zwave") || address.contains("web")) {
            deviceBean = SystemConstant.deviceBeanHashMap.get(address);
            if (deviceBean != null) {
                holder.tvDeviceName.setText(deviceBean.getName());
            }
            //根据Type显示不同的Icon
            if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_PH) {
                holder.ivDeviceIcon.setImageResource(R.drawable.ph_icon);
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_TEMPERATURE) {
                holder.ivDeviceIcon.setImageResource(R.drawable.temperature_icon);
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_SWITCH) {
                holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
                holder.ivDeviceIcon.setImageResource(R.drawable.water_lever_icon);
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_FEEDER) {
                holder.ivDeviceIcon.setImageResource(R.drawable.feed_icon);
            } else {
                holder.ivDeviceIcon.setImageResource(R.drawable.ictest);
            }
            holder.iv_modify_device.setTag(deviceBean);
            holder.iv_delete_device.setTag(deviceBean);
            if (isModify) {
                holder.rl_modify.setVisibility(View.VISIBLE);
                holder.iv_delete_device.setOnClickListener(listener);
                holder.iv_modify_device.setOnClickListener(listener);
            } else {
                holder.rl_modify.setVisibility(View.GONE);
            }
            holder.rlBg.setTag(deviceBean);
            if (longClickListener != null)
                holder.rlBg.setOnLongClickListener(longClickListener);

        } else {
            //摄像头UI设置
            CameraBean cameraBean = SystemConstant.cameraBeanHashMap.get(address);
            if (cameraBean != null) {
                holder.tvDeviceName.setText(cameraBean.getTitle());
            }
            holder.ivDeviceIcon.setImageResource(R.drawable.camera_icon);
            holder.iv_modify_device.setTag(cameraBean);
            holder.iv_delete_device.setTag(cameraBean);
            if (isModify) {
                holder.rl_modify.setVisibility(View.VISIBLE);
                holder.iv_delete_device.setOnClickListener(listener);
                holder.iv_modify_device.setOnClickListener(listener);
            } else {
                holder.rl_modify.setVisibility(View.GONE);
            }
            holder.rlBg.setTag(cameraBean);
            if (longClickListener != null)
                holder.rlBg.setOnLongClickListener(longClickListener);
        }
        return convertView;
    }

    public void setOnclickListener(View.OnClickListener listviewOnClickListener) {
        listener = listviewOnClickListener;
    }

    public void setOnLongClickListener(View.OnLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    /**
     * 刷新鱼缸数据列表
     *
     * @param aquariumBean
     */
    public void refreshData(AquariumBean aquariumBean) {
        this.aquariumBean = aquariumBean;
        addresses = new ArrayList<>();
        for (String address : aquariumBean.getDeivices()) {
            DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(address);
            if(deviceBean != null) {
                if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_PH) {
                } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_TEMPERATURE) {
                } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_SWITCH) {
                } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
                } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_FEEDER) {
                } else {
                    continue;
                }

                addresses.add(address);
            }

        }

//        addresses.addAll(aquariumBean.getDeivices());
        addresses.addAll(aquariumBean.getCameras());
        notifyDataSetChanged();
    }

    /**
     * 进入修改模式
     *
     * @param IsModify
     */
    public void startModify(Boolean IsModify) {
        isModify = IsModify;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        ImageView ivDeviceIcon;
        TextView tvDeviceName;
        RelativeLayout rl_modify;
        ImageView iv_delete_device;
        ImageView iv_modify_device;
        RelativeLayout rlBg;
    }
}
